package com.industry.framework.core.generic.record.dataobject.vo;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.industry.framework.common.constant.Global;
import com.industry.framework.common.redis.RedisUtils;
import com.industry.framework.core.cache.GlobleConstants;
import com.industry.framework.core.generic.record.dataobject.dto.RecordDto;
import com.industry.framework.core.generic.record.dataobject.entity.RecordEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;


@Data
public class RecordVo {

    private static final String GROUP_TODAY = "今天";
    private static final String GROUP_YESTERDAY = "昨天";
    private static final String GROUP_BEFORE_MONTH = "一个月内";
    private static final String GROUP_BEFORE_HALF_YEAR = "六个月内";

    private String dateGroup; //按时间分组 今天，昨天，前天，以前
    private List<RecordDto> recordEntityList;

    public static List<RecordVo> build(List<RecordDto> list){
        final LocalDate now = LocalDate.now();
        final LocalDate yesterday = now.minusDays(1);
        final LocalDate oneMonthAgo = now.minusMonths(1);
        final LocalDate sixMonthsAgo = now.minusMonths(6);
        List<RecordVo> res = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            // 责任链条件列表：按时间顺序从近到远
            List<Map.Entry<Predicate<LocalDate>, String>> handlers = Arrays.asList(
                    createEntry(date -> date.isAfter(sixMonthsAgo) && date.isBefore(oneMonthAgo), GROUP_BEFORE_HALF_YEAR),
                    createEntry(date -> date.isAfter(oneMonthAgo) && date.isBefore(yesterday), GROUP_BEFORE_MONTH),
                    createEntry(date -> date.equals(yesterday), GROUP_YESTERDAY),
                    createEntry(date -> date.equals(now), GROUP_TODAY)
            );
            List<String> sorter = Arrays.asList(
                    GROUP_TODAY,GROUP_YESTERDAY,GROUP_BEFORE_MONTH,GROUP_BEFORE_HALF_YEAR
            );
            Map<String, List<RecordDto>> resMap = list.stream()
                    .collect(Collectors.groupingBy(a ->
                            handlers.stream()
                                    .filter(entry -> entry.getKey().test(a.getCreateTime().toLocalDate()))
                                    .findFirst()
                                    .map(Map.Entry::getValue)
                                    .orElseThrow(() -> new IllegalStateException("未找到日期或日期错误"))));

            //排序 notes:这里用两个map排序，不需要treemap 还尼玛要自定义put 艹
            Map<String, List<RecordDto>> orderedMap = new LinkedHashMap<>();
            sorter.forEach(key ->{
                        if(resMap.containsKey(key)){
                            orderedMap.put(key, resMap.getOrDefault(key, Collections.emptyList()));
                        }
                    }
            );
            orderedMap.forEach((k,v)->{
                RecordVo vo = new RecordVo();
                vo.setDateGroup(k);
                vo.setRecordEntityList(v);
                res.add(vo);
            });
        }
        return res;
    }
    private static <K, V> Map.Entry<K, V> createEntry(K key, V value) {
        return new AbstractMap.SimpleEntry<>(key, value);
    }

}
